import asyncio
import json
from typing import Tuple
import aiohttp
import requests


def csrf_test():
    url = "http://127.0.0.1:8000/protect/flush"
    data = {
        "ip_address": "127.0.0.1",
        "type": "minute",
        "route": "/"
    }
    header = {
        "X-CSRFToken": 'S29ArcL8xrlQdd6J2veqZVPBVerjl5oZ',
        "Content-Type": "application/json",
        "Cookie": "Pycharm-1d570038=cdde3e05-b6a9-4499-a535-0551d162729b; "
                  "csrftoken=S29ArcL8xrlQdd6J2veqZVPBVerjl5oZ; sessionid=4q4t3x6qmp2dreowxyg13n2j0a44wotu"
    }
    resp = requests.post(url, data=json.dumps(data), headers=header)
    if resp.status_code == 200:
        print(resp.text)
    else:
        print(f'failed code: {resp.status_code}\n')


async def test_singleton(session: aiohttp.ClientSession, x: int) -> Tuple[int, str]:
    url = "http://127.0.0.1:8000/hello"
    headers = {
        'Cookie': ('Pycharm-1d570038=cdde3e05-b6a9-4499-a535-0551d162729b; '
                   'csrftoken=6PuRYxbGNlgT6U5bNwp4SyVwy2k54vLf; '
                   'sessionid=9ad2hvn7daz4kz2q8tjsyc44phd682zr'),
        'X-CSRFToken': '6PuRYxbGNlgT6U5bNwp4SyVwy2k54vLf'
    }
    resp = await session.get(url, headers=headers)
    if resp.status == 200:
        return x, await resp.text()
    else:
        return x, f'failed code: {resp.status}'


async def main():
    async with aiohttp.ClientSession() as session:
        result = await asyncio.gather(*[test_singleton(session, i) for i in range(100)])
    counts = set()
    for i, s in result:
        counts.add(json.loads(s).get('count'))
        print(s)
    print(counts, len(counts))


if __name__ == '__main__':
    asyncio.run(main())
